package edu.spec.student.service.impl;

import edu.spec.student.dao.iface.IStudentLessonDao;
import edu.spec.student.model.lesson.StudentLesson;
import edu.spec.student.service.iface.IStudentLessonService;
import edu.spec.basic.model.enumeration.Status;
import org.madgecko.basic.kit.BasicKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentLessonService implements IStudentLessonService {
    @Autowired
    private IStudentLessonDao studentLessonDao;

    @Override
    public List<StudentLesson> listStudentLessonByCon(Integer lessonId, Integer signIn) {
        return studentLessonDao.listStudentLessonByCon(lessonId, signIn);
    }

    @Override
    public Map signIn(Integer studentId, Integer lessonId) {
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean success = true;
        String message = "这堂课没白来，至少签到成功";
        StudentLesson sl = studentLessonDao.loadByStudentLesson(studentId, lessonId);

        if (BasicKit.isEmpty(sl)){
            // 没有学生和课堂关联的情况
            success = false;
            message = "我非常肯定，你进错教室了";
        }else{
            // 状态已经禁止
            if(Status.DISABLED.equals(sl.getLesson().getStatus())){
                success = false;
                message = "糟了，你迟到了，而且老师生气了，不接受扫码了";
            }else if(sl.getSignIn()==0||sl.getSignIn()==null){
                // 第一次扫码
                studentLessonDao.signIn(studentId, lessonId);
            }else if(sl.getSignIn()==1){
                // 已经扫过码的情况
                message = "扫码虽好，请勿贪玩，再重复扫我要生气了";
            }
        }
        map.put("success", success);
        map.put("message", message);
        return map;
    }
}
